package com.qf.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.common.R;
import com.qf.question.entity.Question;
import com.qf.question.mapper.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Daniel
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/bank_service/question")
public class QuestionController {

    @Autowired
    QuestionMapper questionMapper;


    /**
     * 添加问题
     * @param question
     * @return
     */

    @PostMapping("/insert")
    public R addQuestion(@RequestBody Question question){

        if (question!=null){
            questionMapper.insert(question);
        }else {
            System.out.println("没输入问题");
        }
        return R.ok();
    }


    /**
     * 修改问题
     * @param question
     * @return
     */
    @PostMapping("/update")
    public R editQuestion(@RequestBody Question question){
        if (question!=null){
            QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
            QueryWrapper<Question> updateInfo = queryWrapper
                    .eq("pk_qid", question.getPkQid());
            questionMapper.update(question,updateInfo);
        }else {
            System.out.println("没修改问题");
        }
        return R.ok();
    }


    /**
     * 删除问题
     * @param questionId
     * @return
     */
    @GetMapping("/delete")
    public R deleteQuestion(@RequestParam Integer questionId){
        if (questionId!=null){
            questionMapper.deleteById(questionId);
        }else {
            System.out.println("没删除问题");
        }
        return R.ok();
    }


    /**
     * 查询所有问题
     * @return
     */
    @GetMapping
    public R showAll(){
        List<Question> questions = questionMapper.selectList(null);
        if (questions!=null){
            System.out.println("最终结果："+ questions);
            return R.ok();
        }
        return R.error();
    }


    /**
     * 根据id查询问题
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public R findById(@RequestParam Integer id){
        Question question = questionMapper.selectById(id);
        if (question!=null){
            System.out.println("最终结果："+ question);
            return R.ok();
        }
        return R.error();
    }


    @GetMapping("/findByWord")
    public R findByWord(@RequestParam String name){
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Question> uk_title = queryWrapper.like("uk_title", name);
        List<Question> questions = questionMapper.selectList(uk_title);
        System.out.println("最终结果："+ questions);
        return R.ok();
    }






}

